// pages/index/news_tags/news_tags.js
var tool_utils = require('../../../../utils/tool-utils.js')
var httpUtils = require('../../../../utils/https-utils.js')
var app = getApp()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEdit: false,
    my_tags:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    // that.setData({
    //   my_tags: app.data.my_tags,
    //   recom_tags: app.data.recom_tags
    // })

    getData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 栏目编辑切换
  bindEdit: function (e) {
    if (that.data.isEdit) {
      var params = {}
      if (!!app.data.token) {
        params.token = app.data.token
      }else{
        tool_utils.showFailToast("token不存在")
        return
      }
      var StreetInfo = app.getStreetInfo()
      if (StreetInfo && StreetInfo.group_id) {
        params.group_id = StreetInfo.group_id
      }else{
        tool_utils.showFailToast("街道不存在")
        return
      }
      var category_ids = that.data.my_tags.concat();
      category_ids.shift()
      params.category_id = category_ids
      httpUtils.putEditCategory(params,function(res){
        // app.data.my_tags = that.data.my_tags
        // app.data.recom_tags = that.data.recom_tags
        getCurrentPages()[0].setData({
          labelList: that.data.my_tags
        })
      },function(e){
        
      })
    }
    that.setData({
      isEdit: !that.data.isEdit
    })
  },
  // 我的栏目点击跳转
  jump: function (e) {
    if (!that.data.isEdit) {
      var item = e.currentTarget.dataset.item
      tool_utils.pageTo('/page/pack-index/pages/news-list/news-list?title=' + item.category_name + "&category_id=" + item.category_id +"&run_in=1")
    }
  },
  // 推荐栏目点击点击跳转及编辑时点击加入我的栏目
  addTags: function (e) {
    var item = e.currentTarget.dataset.item
    var index = e.currentTarget.dataset.index
    if (that.data.isEdit) {
      var item = that.data.recom_tags.splice(index, 1)
      that.data.my_tags.push(item.shift())
      that.setData(that.data)
    } else {
      tool_utils.pageTo('/page/pack-index/pages/news-list/news-list?title=' + item.category_name + "&category_id=" + item.category_id + "&run_in=1")
    }
  },
  // 删除我的栏目中的栏目
  deleteTags: function (e) {
    var index = e.currentTarget.dataset.index
    var item = that.data.my_tags.splice(index, 1)
    that.data.recom_tags.push(item.shift())
    that.setData(that.data)
  }
})
//获取栏目
function getData() {
  var StreetInfo = app.getStreetInfo()
  //判断街道是否存在
  if (StreetInfo && StreetInfo.group_id) {
    var params = {}
    params.group_id = StreetInfo.group_id
    params.category_type = 1
    if (!!app.data.token) {
      params.token = app.data.token
    }
    httpUtils.getCategory(params, function (res) {
      that.setData({
        my_tags: res.user_category,
        recom_tags: res.category
      })
    }, function (e) {

    })
  } else {
    tool_utils.showFailToast("请选择街道")
  }
}